﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Testing.WinForm
{
    public partial class Form1 : Form
    {
        #region Ctor
        public Form1()
        {
            InitializeComponent();

            label1.Visible = false;
            
        }
        #endregion

        #region ViewMethods
        private void button1_Click(object sender, EventArgs e)
        {
            

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten numeros");
                e.Handled = true;
                return;
            }
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            LengthValidation(textBox1, 8, "Se ingresaron mas de {0} caracteres");

            label4.Text = CountCharacters(textBox1, 8);

        }

        private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            LengthValidation(textBox2, 10, "Se ingresaron mas de {0} caracteres");

            label5.Text = CountCharacters(textBox2, 10);
        }
        #endregion

        #region OurPrivateMethods

        private void LengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length != ctrLength)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);
            label1.Text = "Listado de errores: \n";
            label1.Text += test;

            if (test != "")
            {
                label1.Visible = true;
            }
            else
            {
                label1.Visible = false;
            }

        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;

        }

        private string CountCharacters(Control control, int ctrLength)
        {
            return string.Format("({0}/{1})", control.Text.Length, ctrLength);
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            label4.Text = CountCharacters(textBox1, 8);
            label5.Text = CountCharacters(textBox2, 10);
        }

        private void form1conUCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }

        private void fToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.Show();
        }

        

    }
}
